<template>
	<view class="container">
		<!-- 顶部导航栏 -->
		<view class="nav-bar" :style="{ paddingTop: statusBarHeight + 'px' }">
			<view class="nav-content">
				<text class="nav-title">账单列表</text>
			</view>
		</view>
		
		<!-- 搜索栏 -->
		<view class="search-bar">
			<view class="search-input">
				<text class="iconfont icon-search">🔍</text>
				<input 
					type="text"
					v-model="searchText"
					placeholder="搜索账单"
					placeholder-class="placeholder"
				/>
			</view>
		</view>
		
		<!-- 账单列表 -->
		<scroll-view 
			scroll-y 
			class="bill-list"
			:style="{ height: `calc(100vh - ${statusBarHeight + 44 + 100}px)` }"
		>
			<view 
				class="bill-item"
				v-for="(bill, index) in filteredBills"
				:key="index"
				@click="goToDetail(bill)"
			>
				<view class="bill-left">
					<text class="bill-icon">{{ bill.icon }}</text>
					<view class="bill-info">
						<text class="bill-category">{{ bill.category }}</text>
						<text class="bill-remark">{{ bill.remark }}</text>
						<text class="bill-time">{{ formatTime(bill.time) }}</text>
					</view>
				</view>
				<text class="bill-amount" :class="{ 'income': bill.amount > 0 }">
					{{ bill.amount > 0 ? '+' : '' }}{{ bill.amount }}
				</text>
			</view>
			
			<view class="empty-state" v-if="filteredBills.length === 0">
				<text class="empty-icon">📝</text>
				<text class="empty-text">暂无相关记录</text>
			</view>
		</scroll-view>
	</view>
</template>

<script>
import { mapGetters, mapActions } from 'vuex'

export default {
	data() {
		return {
			statusBarHeight: 0,
			searchText: ''
		}
	},
	computed: {
		...mapGetters(['getAllBills']),
		filteredBills() {
			if (!this.searchText) {
				return this.getAllBills
			}
			const key = this.searchText.toLowerCase()
			return this.getAllBills.filter(bill => 
				bill.category.toLowerCase().includes(key) ||
				bill.remark.toLowerCase().includes(key) ||
				bill.amount.toString().includes(key) ||
				this.formatTime(bill.time).toLowerCase().includes(key)
			)
		}
	},
	onShow() {
		// 每次显示页面时加载账单数据
		this.loadBills()
		console.log('测试master数据');
	},
	methods: {
		...mapActions(['loadBills']),
		formatTime(time) {
			const date = new Date(time)
			const year = date.getFullYear()
			const month = String(date.getMonth() + 1).padStart(2, '0')
			const day = String(date.getDate()).padStart(2, '0')
			const hours = String(date.getHours()).padStart(2, '0')
			const minutes = String(date.getMinutes()).padStart(2, '0')
			const seconds = String(date.getSeconds()).padStart(2, '0')
			return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
		},
		goBack() {
			uni.navigateBack()
		},
		goToDetail(item) {
			uni.navigateTo({
				url: '/pages/detail/detail?data=' + encodeURIComponent(JSON.stringify(item))
			})
		}
	}
}
</script>

<style>
.container {
	min-height: 100vh;
	background: #f8f8f8;
	position: relative;
}

.nav-bar {
	background: linear-gradient(135deg, #6366F1 0%, #8B5CF6 100%);
	position: fixed;
	top: 0;
	left: 0;
	right: 0;
	z-index: 100;
}

.nav-content {
	height: 44px;
	display: flex;
	align-items: center;
	justify-content: center;
}

.nav-title {
	color: #fff;
	font-size: 36rpx;
	font-weight: 500;
}

.search-bar {
	padding: 20rpx 30rpx;
	background: linear-gradient(135deg, #6366F1 0%, #8B5CF6 100%);
	position: fixed;
	top: calc(var(--status-bar-height) + 18px);
	left: 0;
	right: 0;
	z-index: 99;
}

.search-input {
	background: rgba(255, 255, 255, 0.2);
	height: 80rpx;
	border-radius: 40rpx;
	display: flex;
	align-items: center;
	padding: 0 30rpx;
}

.icon-search {
	color: #fff;
	font-size: 36rpx;
	margin-right: 20rpx;
}

.search-input input {
	flex: 1;
	height: 100%;
	font-size: 28rpx;
	color: #fff;
}

.placeholder {
	color: rgba(255, 255, 255, 0.8);
}

.bill-list {
	position: fixed;
	top: calc(var(--status-bar-height) + 44px + 100rpx);
	left: 0;
	right: 0;
	bottom: 0;
	padding: 20rpx;
	box-sizing: border-box;
}

.bill-item {
	background: #fff;
	border-radius: 16rpx;
	padding: 30rpx;
	margin-bottom: 20rpx;
	display: flex;
	justify-content: space-between;
	align-items: center;
	box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
}

.bill-left {
	display: flex;
	align-items: center;
}

.bill-icon {
	width: 80rpx;
	height: 80rpx;
	background: #f8f8f8;
	border-radius: 40rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	font-size: 32rpx;
	margin-right: 20rpx;
}

.bill-info {
	display: flex;
	flex-direction: column;
}

.bill-category {
	font-size: 28rpx;
	color: #333;
	margin-bottom: 4rpx;
}

.bill-remark {
	font-size: 24rpx;
	color: #999;
	margin-bottom: 4rpx;
}

.bill-time {
	font-size: 22rpx;
	color: #999;
}

.bill-amount {
	font-size: 32rpx;
	color: #f43f5e;
	font-weight: 500;
}

.bill-amount.income {
	color: #10B981;
}

.empty-state {
	padding: 120rpx 0;
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
}

.empty-icon {
	font-size: 80rpx;
	margin-bottom: 20rpx;
}

.empty-text {
	font-size: 28rpx;
	color: #999;
}
</style>